package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.UserInfoDTO;
import net.wanho.po.AuthInfo;
import net.wanho.po.Role;
import net.wanho.po.UserInfo;
import net.wanho.service.UserInfoService;
import net.wanho.util.CurrentUtil;
import net.wanho.vo.*;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserInfoController {
    private final UserInfoService userInfoService;

    /**
     *用户列表
     */
    @GetMapping("/user-list")
    public R getUserInfo(@RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "5") Integer pageSize,
                         UserInfoDTO userInfoDTO) {
        PageInfo<UserInfo> pageInfo = userInfoService.listPage(pageNum, pageSize, userInfoDTO);
        return R.success(pageInfo);

    }
    /**
     *菜单列表
     */
    @GetMapping("/auth-list")
    public R getMenu() {
        List<AuthVO> authVOList =userInfoService.getAuth();
        return R.success(authVOList);
    }
    /**
     * 添加用户
     */
    @PostMapping("/addUser")
    public R addUser(@RequestBody UserInfoVo userInfoVo) {
        userInfoService.addUser(userInfoVo);
        return R.success("添加用户成功");
    }
    /**
     * 修改用户状态
     */
    @PutMapping("/updateState")
    public R updateState(@RequestBody UserInfo userInfo) {
        userInfo.setUpdateTime(new Date());
        userInfo.setUpdateBy(CurrentUtil.getUserId());
        userInfoService.updateById(userInfo);
        return R.success("修改成功");
    }
    /**
     * 根据id查询用户信息
     */
    @GetMapping("/{id}")
    public R getUserInfoById(@PathVariable("id") Integer id) {
        UserInfo userInfo = userInfoService.getById(id);
        return R.success(userInfo);
    }
    /**
     * 修改用户信息
     */
    @PutMapping("/updateUser")
    public R updateUser(@RequestBody UserInfo userInfo) {
        userInfo.setUpdateTime(new Date());
        userInfo.setUpdateBy(CurrentUtil.getUserId());
        userInfoService.updateById(userInfo);
        return R.success("用户修改成功");
    }
    /**
     * 删除用户
     */
    @DeleteMapping("/deleteUser/{id}")
    public R deleteUser(@PathVariable("id") Integer id) {
        userInfoService.removeById(id);
        return R.success("用户删除成功");
    }
    /**
     * 批量删除用户
     */
    @DeleteMapping("/deleteUserList")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        userInfoService.removeByIds(ids);
        return R.success("用户批量删除成功");
    }
    /**
     * 重置密码
     */
    @PutMapping("/updatePwd/{id}")
    public R resetPwd(@PathVariable Integer id) {
        userInfoService.updatePwd(id);
        return R.success("密码重置成功");
    }
    /**
     * 根据用户ID查询角色
     */
    @GetMapping("/user-role-list/{id}")
    public R getUserRoleList(@PathVariable Integer id) {
        List<Role> roleList = userInfoService.getUserRoleList(id);
        return R.success(roleList);
    }

    @PutMapping("/assignRole")
    public R assignRole(@RequestBody UserInfoVo2 userInfoVo2) {
        userInfoService.updateRole(userInfoVo2);
        return R.success("分配角色成功");
    }
    @GetMapping("/user-auth")
    public R getUserInfo(Integer userId) {
        List<Integer> authIdList = userInfoService.getUserAuthIdList(userId);
        return R.success(authIdList);
    }

    /**
     * 导出数据
     */
    /**
     * 导出数据
     */
    @GetMapping("/exportTable")
    public R exportTable(@RequestParam(defaultValue = "1") int pageNum,
                         @RequestParam(defaultValue = "5") int pageSize,
                         UserInfoDTO userInfoDTO){
        PageInfo<UserInfo> pageInfo = userInfoService.listPage(pageNum, pageSize, userInfoDTO);
        return R.success(pageInfo.getResultList());
    }

}
